var fs = require('fs');
module.exports = Controller(function () {
    'use strict';
    return {

        commonjsAction: function () {
            //公共配置项输出
            //console.log(APP_PATH);
            //console.log(this.http)
            var con=C("setting_default");
            //console.log(con);
            var config = {
                "lang": "zh_CN",
                "is_root": 0,
                "web_root": "/",
                "web_host":"http://"+this.http.host,
                "static_path": ".\/static\/",
                "basic_path": BASIC_PATH,
                "version": CW_VERSION,
                "app_host": "http:\/\/127.0.0.1\/kod\/",
                "office_server": "",
                "json_data": "",
                "share_page": "share",
                "theme": con.theme,
                "list_type": con.list_type,
                "sort_field": con.list_sort_field,
                "sort_order": con.list_sort_order,
                "musictheme": con.musictheme,
                "movietheme": con.movietheme
            };
            var data = JSON.stringify(F("main", undefined, RESOURCE_PATH + "/data/i18n/zh_CN"));
            var js;
            js = "LNG=" + data + ";";
            js += "AUTH=[];";
            js += "G=" + JSON.stringify(config) + ";";
            this.type("js");
            this.json(js);

        },
        registerAction: function () {
            var self = this;

            //var password1 = this.post("rpassword1");
            console.log(name);
            if (this.isAjax()) {
                var name = this.post("username");
                var email = this.post("email");
                var password = this.post("password");
                var data = {
                    username: name,
                    email: email,
                    password: md5(password)
                }
                var where = {
                    username: name,
                    email: email,
                    _logic: 'OR'
                }
                D("user").thenAdd(data, where, true).then(function (data) {
                    //console.log(data)
                    if (data.type == "add") {
                        self.userdir("uid_" + data.id);
                    }
                    self.json(data);
                    //return self.redirect("/Explorer");
                })

            } else {
                self.error("参数不合法");
            }

        },
        checkgetAction: function () {
            var self = this;
            if (this.isAjax()) {
                var username = this.get("username");
                var email = this.get("email");
                console.log(email)
                if (username) {
                    D("user").where({username: ['like', username]}).find().then(function (data) {
                        if (data.username) {
                            self.json("1");
                        } else {
                            self.json("2");
                        }
                    });
                }
                if (email) {
                    D("user").where({email: ['like', email]}).find().then(function (data) {
                        if (data.email) {
                            self.json("1");
                        } else {
                            self.json("2");
                        }
                    });
                }


                //console.log();
                //D("user").where({})
            }
        },
        userdir: function (uid) {//用户注册时，初始化用户目录

            var self = this;
            var config_data = {
                "list_type": "icon",
                "list_sort_field": "name",
                "list_sort_order": "up",
                "theme": "simple\/",
                "codetheme": "clouds",
                "wall": "6",
                "musictheme": "mp3player",
                "movietheme": "webplayer"
            };
            var editor_config_data = {
                "font_size": "15px",
                "theme": "clouds",
                "auto_wrap": 0,
                "display_char": 0,
                "auto_complete": 1,
                "function_list": 1
            };
            var fav_data = {};
            var share_data = {};
            var t = {
                "isDir": true, "name": uid, "child": [
                    {
                        "isDir": true, "name": "data", "child": [
                        {isDir: true, "name": "temp", "child": []},
                        {isDir: false, "name": "config.json", "data": config_data},
                        {isDir: false, "name": "editor_config.json", "data": editor_config_data},
                        {isDir: false, "name": "fav.json", "data": fav_data},
                        {isDir: false, "name": "share.json", "data": share_data}
                    ]
                    },
                    {
                        "isDir": true, "name": "home", "child": [
                        {
                            "isDir": true, "name": "desktop", "child": [
                            {
                                "isDir": false,
                                "name": "365日历.oexe",
                                "data": {
                                    "type": "url",
                                    "content": "http:\/\/baidu365.duapp.com\/wnl.html?bd_user=855814346&bd_sig=a64e6e262e8cfa1c42dd716617be2102&canvas_pos=platform",
                                    "icon": "365.png",
                                    "width": "544",
                                    "height": "440",
                                    "simple": 0,
                                    "resize": 1
                                }
                            },
                            {
                                "isDir": false,
                                "name": "icloud.oexe",
                                "data": {
                                    "type": "app",
                                    "content": "window.open(\"https:\/\/www.icloud.com\/\");",
                                    "icon": "icloud.png",
                                    "width": "800",
                                    "height": "600",
                                    "simple": 0,
                                    "resize": 1
                                }
                            }


                        ]
                        },
                        {"isDir": true, "name": "download", "child": []},
                        {"isDir": true, "name": "image", "child": []},
                        {"isDir": true, "name": "music", "child": []}
                    ]
                    },
                    {"isDir": true, "name": "recycle", "child": []}

                ]
            };

            function visitDir(fileObj, oldpath) {
                if (fileObj.isDir) {
                    var dir = fileObj.name;
                    oldpath += dir;
                    oldpath += "/";
                    console.log(oldpath + "这是目录");
                    creatdir("User/" + oldpath).then(function (dd) {
                        console.log(dd);

                    });
                    if (fileObj.child.length > 0) {
                        for (var t in fileObj.child) {
                            visitDir(fileObj.child[t], oldpath);
                        }
                    }
                    else
                        return false;
                } else {

                    oldpath += fileObj.name;
                   // console.log(fileObj.data);
                   // console.log(oldpath + "这是文件");
                   var datas = JSON.stringify(fileObj.data);
                    console.log(datas+"ddddddddddd");
                    fs.writeFile(RESOURCE_PATH + "/User/" + oldpath, datas, function (err) {
                        if (err) throw err;
                        console.log('It\'s saved!'); //文件被保存
                    });
                    return false;
                }

            }

            if (uid) {
                visitDir(t, "");
            }


        }

    }
});